//获取每段路名
function getRoadName(route) {
    var pattern1 = "沿[\u4e00-\u9fa5]+行驶";
    var pattern2 = "进入[\u4e00-\u9fa5]+";
    var names=new Array(route.getNumSteps());

    for (var i = 1; i < route.getNumSteps(); i++) {
        var step = route.getStep(i);
        var contents= step.getDescription();
        var re1 = contents.match(pattern1);
        var re2 = contents.match(pattern2);
        if (re1==null){
            names[i-1]="无名路";
            continue;
        }
        if(re1.length>=1){
            var s1=re1[0].replace("沿","").replace("行驶","");
            names[i-1]=s1;
        }
        if (re2==null){
            continue;
        }
        if(re2.length>=1) {
            var s2 = re2[0].replace("进入", "");
        }
    }
    return names;
}

//将一段route根据step的关键点分成若干个路段
function segmentedRoad(route) {
    // var segmentedPoints=new Array(route.getNumSteps());//分割点
    var segmentedPoints=[];//分割点
    var points=route.getPath();//构成线路的所有几何点
    for (var i = 0; i < route.getNumSteps(); i++) {
        var step = route.getStep(i);
        // segmentedPoints[i]=step.getPosition();
        segmentedPoints.push(step.getPosition());
    }
    var index=0;
    var roadIndex=0;
    var flag=1;
    var roadPoint=[];
    var road=[];

    var names=getRoadName(route)

    for (var point of points)
    {

        if (index==route.getNumSteps())
        {
            break;
        }
        if (point.lng==segmentedPoints[index].lng&&point.lat==segmentedPoints[index].lat){
            if (flag==1){
                roadPoint=[];
                roadPoint.push(point);
                flag=2;
                index++
            }
            else if (flag==2){
                roadPoint.push(point);
                road.push({"index":roadIndex,"points":roadPoint,"name":names[roadIndex]});
                roadIndex++;
                flag=1;
            }

        }
        else {
            roadPoint.push(point);
        }

    }
    return road;
}